package model

import (
	"gorm.io/gorm"
	"group_-no.2/utils/errmsg"
)

type Article struct {
	Category Category `gorm:"foreignKey:CategoryID"`
	gorm.Model
	Title        string `gorm:"type:varchar(100);not null" json:"title"`
	CategoryID   uint   `gorm:"type:int;not null" json:"category_id"`
	Desc         string `gorm:"type:varchar(200)" json:"desc"`
	Content      string `gorm:"type:longtext" json:"content"`
	Img          string `gorm:"type:varchar(100)" json:"img"`
	ReadCount    int    `gorm:"type:int;default(0)" json:"read_count"`
	CommentCount int    `gorm:"type:int;default(0)" json:"comment_count"`
}

type ArticleResult struct {
	ID           uint64 `json:"article_id"`
	Title        string `json:"title"`
	Desc         string `json:"desc"`
	Content      string `json:"content"`
	CategoryID   uint   `json:"category_id"`
	ReadCount    int    `json:"read_count"`
	CommentCount int    `json:"comment_count"`
	CategoryName string `json:"category_name"`
	CreateTime   string `json:"create_time"`
	UpdateTime   string `json:"update_time"`
}

// 添加文章
func CreateArticle(data *Article) (code int) {
	err := db.Create(&data).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}

// 编辑文章
func EditArticle(id uint, data *Article) int {
	var cate Article
	var maps = make(map[string]interface{})
	maps["title"] = data.Title
	maps["category_id"] = data.CategoryID
	maps["desc"] = data.Desc
	maps["content"] = data.Content

	err = db.Model(&cate).Where("id = ? ", id).Updates(maps).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}

// 删除文章
func DeleteArticle(id uint) int {
	var art Article
	err = db.Where("id = ? ", id).Delete(&art).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}

// 查询单个文章
func GetArticleInfo(id uint) (Article, int) {
	var art Article
	err := db.Where("id = ? ", id).First(&art).Error
	db.Model(&Article{}).
		Where("id = ? ", id).
		UpdateColumn("read_count", gorm.Expr("read_count + ?", 1))
	if err != nil {
		return art, errmsg.ERROR_ART_NOT_EXIST
	}
	return art, errmsg.SUCCESS
}

// 查询文章列表
func GetArticleList(pageSize int, pageNum int) ([]ArticleResult, int64, int) {
	var art []ArticleResult
	var total int64

	err := db.Select("article_id, title, desc, content, category_id, " +
		"category_name, read_count, comment_count, update_time, create_time").
		Order("update_time desc").
		Joins("left join category on article.category_id = category.id").
		Offset((pageNum - 1) * pageSize).
		Limit(pageSize).
		Scan(&art).Error
	db.Model(&art).Count(&total)

	if err != nil {
		return nil, 0, errmsg.ERROR
	}

	return art, total, errmsg.SUCCESS
}

// 查询分类下的文章
func GetArticleListUnderCategory(id uint, pageSize int, pageNum int) ([]ArticleResult, int64, int) {
	var art []ArticleResult
	var total int64

	err := db.Select("article_id, title, desc, content, category_id, "+
		"read_count, comment_count, update_time, create_time").
		Order("update_time desc").
		Joins("left join category on article.category_id = category.id").
		Offset((pageNum-1)*pageSize).
		Limit(pageSize).
		Where("category_id = ? ", id).
		Scan(&art).
		Error

	db.Model(&art).Count(&total)

	if err != nil {
		return nil, 0, errmsg.ERROR
	}

	return art, total, errmsg.SUCCESS
}

// 按照文章名称查询
func SearchArticle(title string, pageSize int, pageNum int) ([]ArticleResult, int64, int) {
	var art []ArticleResult
	var total int64
	err := db.Select("article_id, title, desc, content, category_id, "+
		"read_count, comment_count, update_time, create_time").
		Order("update_time desc").
		Joins("left join category on article.category_id = category.id").
		Where("title like ?", "%"+title+"%").
		Offset((pageNum - 1) * pageSize).
		Limit(pageSize).
		Scan(&art).
		Error

	db.Model(&art).Count(&total)

	if err != nil {
		return nil, 0, errmsg.ERROR
	}

	return art, total, errmsg.SUCCESS
}
